package com.lyn.usbfile;

import android.annotation.SuppressLint;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

import me.jahnen.libaums.core.UsbMassStorageDevice;

public class UsbDeviceAdapter extends BaseAdapter {

    private final List<UsbMassStorageDevice> usbMassStorageDevices =new ArrayList<>();
    private final Context context;

    public UsbDeviceAdapter(Context context) {
        this.context = context;
    }

    public void setUsbMassStorageDevices(List<UsbMassStorageDevice> usbMassStorageDevices){
        this.usbMassStorageDevices.clear();
        if (usbMassStorageDevices!=null){
            this.usbMassStorageDevices.addAll(usbMassStorageDevices);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return usbMassStorageDevices.size();
    }

    @Override
    public UsbMassStorageDevice getItem(int position) {
        return usbMassStorageDevices.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView==null){
            holder=new ViewHolder();
            convertView= LayoutInflater.from(context).inflate(R.layout.adapter_usbdevice,parent,false);
            holder.name=convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        UsbMassStorageDevice usbMassStorageDevice = usbMassStorageDevices.get(position);
        UsbDevice usbDevice = usbMassStorageDevice.getUsbDevice();
        holder.name.setText(usbDevice.getManufacturerName());
        return convertView;
    }


    static class ViewHolder{
        TextView name;
    }
}
